<?php

namespace app\admin\controller;

//属性页
use app\admin\logic\GoodsPropLogic;
use app\BaseController;
use think\facade\Request;
use think\facade\View;

class GoodsProp extends BaseController
{
    //prop  属性列表
    public function goodProp()
    {
        //where语句拼接
        list($where, $whereOr) = GoodsPropLogic::propWhere();
        //留值
        $data = GoodsPropLogic::propData();
        View::assign($data);
        //属性sql语句
        $prop = \app\admin\model\GoodsProp::propList($where, $whereOr);
        //属性值变中文输出
        $prop = GoodsPropLogic::propValues($prop);
        //属性值复选框
        $prop_value = \app\admin\model\GoodsProp::propValue();
        View::assign('prop_value', $prop_value);
        View::assign('prop', $prop);
        return View::fetch();
    }

    //prop  属性添加
    public function goodsPropAdd()
    {
        if (Request::isPost()) {
            $post = Request::post();
            if (isset($post['prop_value']) && !empty($post['prop_value'])) {
                $prop_value = explode(",", $post['prop_value']);
                $pvlues = \app\admin\model\GoodsProp::goodPropValue($prop_value);
            }
            unset($post['prop_value']);
            if (isset($post['pvalues']) && isset($pvlues)) {
                $post['pvalues'] = array_merge($post['pvalues'], $pvlues);
                $post['pvalues'] = implode(",", $post['pvalues']);
            } elseif (!empty($post['pvalues'])) {
                $post['pvalues'] = implode(",", $post['pvalues']);
            } elseif (!empty($pvlues)) {
                $post['pvalues'] = $pvlues;
                $post['pvalues'] = implode(",", $post['pvalues']);
            }else{
                $post['pvalues'] = '';
            }
            $res = \app\admin\model\GoodsProp::goodPropAddInfo($post);
            if ($res) {
                $this->success('添加成功', url('goodProp'));
            } else {
                $this->error('添加失败');
            }
        } else {
            return View::fetch();
        }


    }

    //prop 属性软删除
     public function goodsPropDel()
     {
         $get = Request::get();
         $res = \app\admin\model\GoodsProp::goodPropDelInfo($get);
         if ($res) {
             $this->success('删除成功', url('goodProp'));
         } else {
             $this->error('删除失败');
         }

     }

     //prop 属性修改
     public function goodsPropEdit(){
        if (Request::isPost()){
            $param=Request::param();
            if (isset($param['prop_value']) && !empty($param['prop_value'])) {
                $prop_value = explode(",", $param['prop_value']);
                $pvlues = \app\admin\model\GoodsProp::goodPropValue($prop_value);
            }
            unset($param['prop_value']);
            if (isset($param['pvalues']) && isset($pvlues)) {
                $param['pvalues'] = array_merge($param['pvalues'], $pvlues);
                $param['pvalues'] = implode(",",$param['pvalues']);
            } elseif (!empty($param['pvalues'])) {
                $param['pvalues'] = implode(",", $param['pvalues']);
            } elseif (!empty($pvlues)) {
                $param['pvalues'] = $pvlues;
                $param['pvalues'] = implode(",", $param['pvalues']);
            }
            $res = \app\admin\model\GoodsProp::goodPropEditInfo($param);
            if ($res) {
                $this->success('修改成功', url('goodProp'));
            } else {
                $this->error('修改失败');
            }
        }else{
            $param=Request::param();
            $find=\app\admin\model\GoodsProp::goodPropEditInfo($param);
            View::assign($find->toArray());
            //属性值复选框
            $prop_value = \app\admin\model\GoodsProp::propValue();
            View::assign('prop_value', $prop_value);
            return View::fetch();
        }


      }



}